home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_09_04 / 9n04076a < prev    next >
Text File  |  1991-02-17  |  2KB  |  88 lines

  1.  
  2. /********************************************************************/
  3. /*                Test the cheak_heap class.                             */
  4. /********************************************************************/
  5.  
  6. #include <iostream.h>
  7. #include <chkheap.hpp>
  8.  
  9. struct test_class_bad        // Class that does not deallocate.
  10. {
  11.     char *p;
  12.     test_class_bad()    { p = new char; }
  13.     ~test_class_bad()    { /* p is not deleted. */ }
  14. private:
  15.     test_class_bad(test_class_bad&);
  16. };
  17.  
  18. struct test_class_good        // Class that does deallocate.
  19. {
  20.     char *p;
  21.     test_class_good() { p = new char; }
  22.     test_class_good(test_class_good &t) { p = new char; *p = *(t.p); }
  23.     ~test_class_good()    { delete p; }
  24. };
  25.  
  26. char *test_easy(const int);        // Prototypes for test functions.
  27. char *test_class(const int);
  28. void test_value(test_class_good);
  29. void test_test_value(test_class_good &t)    { test_value(t); }
  30.  
  31.  
  32. main()
  33. {
  34.     test_class_good t;
  35.     char *p;
  36.  
  37.     cout << "Testing check_heap class.  Should have three okay errors\n";
  38.     check_heap check;
  39.  
  40.     p = test_easy(1);
  41.     check.test("Test_easy(1) error: ");
  42.  
  43.     p = test_easy(0);
  44.     check.test("This error is okay: ");
  45.     delete p;    // Clean up memory and fix for next check.test().
  46.  
  47.     p = test_class(1);
  48.     check.test("Test_class(1) error: ");
  49.  
  50.     p = test_class(0);
  51.     check.test("This error is okay: ");
  52.     delete p;        // Clean up memory.
  53.  
  54.     check.start();    // Get ready for next call to check.test().
  55.  
  56.     // Next line demonstrates compiler creating temp value.
  57.     test_value(t);
  58.     check.testnew("This error is okay: ");
  59.     test_test_value(t);
  60.     check.test("Test_test_value(t) error: ");
  61. }
  62.  
  63. char *test_class(const int i)
  64. {
  65.     char *r;
  66.  
  67.     if (i)
  68.         {
  69.         test_class_good t;
  70.         r = 0;
  71.         }
  72.     else{
  73.         test_class_bad t;
  74.         r = t.p;
  75.         }
  76.     return r;
  77. }
  78.     
  79. char *test_easy(const int i)
  80. {
  81.     char *p = new char;
  82.     if (i)    { delete p;  p = 0; }
  83.     return p;
  84. }
  85.  
  86. void test_value(test_class_good t)
  87. {    /* Ignore warning about t not being used. */ }
  88.